package com.openlab.action;

import com.openlab.activity.R;
import com.openlab.connection.ConnectThread;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;

public class LoggingAction {
	private String webServiceLoginMethod = ""; 
	private String userName = "";
	private String userPwd = "";
	private Boolean savePwd = false;
	private Context loginContext;
	private Editor confEditor;


	
	public LoggingAction(SharedPreferences sp, Context context){
		this.loginContext = context;		
		confEditor = sp.edit();
		webServiceLoginMethod = context.getResources().getString(R.string.loginMethod);
	}
	
	public void submit(Bundle webSerivceParams, Boolean savePwd, Handler handler){	
		this.savePwd = savePwd;
		ConnectThread connect = new ConnectThread(loginContext);
		connect.doStart(handler, webServiceLoginMethod, webSerivceParams);
	}
	
	public boolean checkResult(String result){
		//save user name and password
		if(result.equals("Success")){
	        if(savePwd){    
	        	confEditor.putString("savePwd", "true");
	        	confEditor.putString("userName", userName);
	        	confEditor.putString("userPwd", userPwd);
	        	confEditor.commit();
	        }else{
	        	confEditor.putString("savePwd", "false");
	        	confEditor.putString("userName", "");
	        	confEditor.putString("userPwd", "");
	        	confEditor.commit();
	        }
	        return true;
		}else{
			return false;
		}
	}
}
